from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app import crud, services
from app.database import get_db
from app.utils import security
from app.schemas.conversation import Conversation, ConversationCreate
from app.schemas.user import User

router = APIRouter(tags=["chat"])

@router.post("/chat", response_model=Conversation)
async def chat(
    conversation: ConversationCreate,
    db: Session = Depends(get_db),
    current_user: User = Depends(security.get_current_user)
):
    # 调用DeepSeek API获取回答
    answer = await services.deepseek.get_deepseek_response(conversation.question)
    
    # 保存对话记录
    db_conversation = crud.conversation.create_conversation(
        db=db,
        conversation=ConversationCreate(
            user_id=current_user.id,
            question=conversation.question,
            answer=answer
        )
    )
    return db_conversation